/* 
 * File:   RealParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 12:26
 */

#include "RealParameterCommand.h"
#include "model/Context.h"
#include "../../memento/ConstantTypeParameterMemento.h"

namespace calculator {

    RealParameterCommand::RealParameterCommand(const std::string name) : ParametersCommand(name) {
    }

    RealParameterCommand::RealParameterCommand(const RealParameterCommand& orig) : ParametersCommand(orig._name) {
    }

    RealParameterCommand::~RealParameterCommand() {
    }

    RealParameterCommand* RealParameterCommand::clone() const {
        return new RealParameterCommand(_name);
    }

    void RealParameterCommand::apply(Parameters* parameters) const {
        parameters->setConstantType(Parameters::REAL);
    }

    const Memento* RealParameterCommand::createMemento() const throw (CommandException) {
        return new ConstantTypeParameterMemento(clone(), Context::getInstance()->getParameters()->getConstantType());
    }

    void RealParameterCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const ConstantTypeParameterMemento* constantTypeParameterMemento = dynamic_cast<const ConstantTypeParameterMemento*> (memento);
        if (constantTypeParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : ConstantTypeParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setConstantType(constantTypeParameterMemento->_constantType);
    }
}